<template>
  <div>
    <!--面包屑区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--搜索区-->
    <el-row :gutter="20">
      <!--搜索区-->
      <el-col :span="8">
        <el-input placeholder="请输入内容"
                  clearable
                  @clear="getGoodsList"
                  v-model="queryInfo.query">
          <el-button @click="getGoodsList"
            slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
      <!--添加商品按钮-->
      <el-col :span="5">
        <el-button type="primary" @click="toAddPage">添加商品</el-button>
      </el-col>
    </el-row>

    <el-table :data="goodsList" border stripe>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column label="商品名称" prop="goods_name"></el-table-column>
      <el-table-column label="商品价格（元）" width="100px" prop="goods_price"></el-table-column>
      <el-table-column label="商品数量" width="70px" prop="goods_number"></el-table-column>
      <el-table-column label="商品重量" width="70px" prop="goods_weight"></el-table-column>
      <el-table-column label="创建时间" width="140px">
        <template slot-scope="scope">
          {{ scope.row.add_time | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="130px">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页区域-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  export default {
      data() {
           return {
              queryInfo: {
                  // 查询参数
                  query: '',
                  // 当前页码
                  pagenum: 1,
                  // 每页显示条数
                  pagesize: 5
              },
               goodsList: [],
               total: 0
           }
      },
      created() {
          this.getGoodsList()
      },
      methods: {
          // 获取商品列表
          async getGoodsList() {
              const { data: res } =  await this.$http.get('goods', {params: this.queryInfo})
              if (res.meta.status !== 200) {
                  return  this.$message.error('获取商品列表失败')
              }
              this.goodsList = res.data.goods
              this.total =res.data.total
              console.log(this.goodsList)
          },
          handleSizeChange(newSize) {
              this.queryInfo.pagesize = newSize
              this.getGoodsList()
          },
          handleCurrentChange(currentPage) {
              this.queryInfo.pagenum = currentPage
              this.getGoodsList()
          },
          toAddPage () {
              this.$router.push('/goods/add')
          }

      }
  }
</script>

<style scoped>

</style>
